var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var search_5_CONST_AD_TIME = 40;
var search_5 = {
  is_over_today: false,
  has_search_times: 0,
  go_page_from_home() {
    var logger = utils.create_logger("search_5 go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger("");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      if (this.is_over_today) {
        logger("今天的-随心搜-已完成");
        this.next();
        return;
      }
      sleep(utils.get_random_interval(1 * 1000, 0, 1000, 1));
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    var logger = utils.create_logger("search_5 init: ");
    in_task.init(null, null, null, null, null, null, 3);
    var ocr_text = ocr_utils.find_text(
      "随心搜",
      false,
      null,
      6,
      (device.height / 10) * 4
    );
    if (ocr_text) {
      var centerX = ocr_text.bounds.centerX();
      var centerY = ocr_text.bounds.centerY();
      this.run(centerX, centerY);
      return;
    }
    logger("没找到-随心搜-，今天的-随心搜-已完成");
    this.is_over_today = true;
    this.after();
    return;
  },
  run(x, y) {
    var logger = utils.create_logger("search_5 run: ");
    utils.click_x_y(x, y);
    var interval = utils.get_random_interval(12, 0, 1, 1000);
    logger(
      "下面将看第-" +
        (this.has_search_times + 1) +
        "-次搜索结果,花费时间-" +
        interval / 1000 +
        "-秒"
    );
    sleep(interval);
    back();
    sleep(utils.get_random_interval(2, 0, 1, 1000));
    if (utils.click_text_starts_with("残忍离开")) {
      logger("找到-残忍离开-");
      sleep(utils.get_random_interval(2, 0, 1, 1000));
    }
    var ocr_text = ocr_utils.screen_exists_text_one(
      ["马上收下", "立即收下"],
      true
    );
    if (ocr_text) {
      console.log("search_5 after: 找到-马上收下或立即收下-");
      utils.click_ocr_text(ocr_text);
    }
    sleep(utils.get_random_interval(2, 0, 1, 1000));
    this.has_search_times = this.has_search_times + 1;
    if (this.has_search_times == 5) {
      logger("没找到-随心搜-，今天的-随心搜-已完成");
      this.is_over_today = true;
      this.after();
      return;
    }
    this.after();
  },
  back_top(times) {
    var logger = utils.create_logger("search_5 back_top: ");
    logger("下面开始回到顶部，将向上滑动--" + times + "--次");
    while (times > 0) {
      swipe(
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 1 * utils.get_swipe_times(95, 5),
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 2 * utils.get_swipe_times(95, 5),
        utils.get_random_interval(200, 0, 500, 1)
      );
      times = times - 1;
      console.log("还需要向上滑动--" + times + "--次");
      sleep(utils.get_random_interval(1, 0, 1, 1000));
    }
  },
  after() {
    console.log("search_5 after:");
    var ocr_text = ocr_utils.screen_exists_text_one("马上收下", true);
    if (ocr_text) {
      console.log("search_5 after: 找到-|马|上|收|下|-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    this.back_top(6);
    console.log("search_5 next");
  },
};
module.exports = search_5;
